<template>
	<view>
		<view class="user_data">
			<u-skeleton rows="2" :loading="loading" avatar avatarSize='120' :title="false" :rowsHeight="[100,20]"
				:rowsWidth="[200,200]" style="width: 100%;height: 100%;">
				<view class="user_main_data">
					<view class="user_img">
						<view class="user_img_redios">
							<image :src="userData.avatar_url" style="width: 100%;height: 100%;"></image>
						</view>
					</view>
					<view class="user_titel">
						<view class="user_titel_main">
							{{userData.name || "尚未登录"}}
						</view>
						<view class="user_titel_center" v-show="!loading">
							邮箱:{{userData.email}}
						</view>
						<view class="user_titel_timer" v-show="!loading">
							创建日期:{{userData.created_at}}
						</view>
					</view>
				</view>
			</u-skeleton>

		</view>
		<view v-show="!loading">
			<view class="my_options" @click="changRouterInfo">
				<view class="my_options_left">
					<uni-icons type="info" class='iconmargin'></uni-icons>
					<text>修改信息</text>
				</view>
				<view class="my_options_right">
					<uni-icons type="right"></uni-icons>
				</view>

			</view>
			<view class="my_options">
				<view class="my_options_left">
					<uni-icons type="star" class='iconmargin'></uni-icons>
					<text>商品收藏</text>
				</view>
				<view class="my_options_right">
					<uni-icons type="right"></uni-icons>
				</view>

			</view>
		</view>
		<view class="my_options" style="margin-top: 450rpx;"
			:style="{'background-color':isLogin ?  '#44C08A' : '#EC6A71'}">
			<view style="display: flex;justify-content: center;align-items: center;width: 100%; color: white;"
				@click="quitLogin">
				<text>{{isLogin ? "登录" :"退出登录"}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		saveToken
	} from '../../tools/token.js'
	import {
		mapActions,
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				loading: false,
				isLogin: true
			};
		},
		async onShow() {
			if (this.isLogin && this.tools.isToken()) {
				this.isLogin = false
				this.loading = true
				const result = await this.getUserData(this)
				this.loading = false
			}
		},
		methods: {
			...mapActions('indexStore', ['getUserData']),
			...mapMutations('indexStore', ['SETUSERDATA','SETTOKEN']),
			changRouterInfo() {
				if (!this.isLogin) {
					uni.navigateTo({
						url: '../../pagesLogin/changUserInfo/changUserInfo'
					})
				} else {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
				}
			},
			quitLogin() {
				if (!this.isLogin) {
					uni.showModal({
						title: '确定退出吗',
						cancelText: '让我再考虑一下',
						confirmText: '是的',
						success: async (res) => {
							if (res.confirm) {
								const result = await uni.$u.api.reqQuitLogin()
								uni.setStorageSync('Bearer', '')
								Object.assign(this.$data, this.$options.data())
								this.SETUSERDATA('')
								this.SETTOKEN('')
							}
						},
					});
				} else {
					uni.navigateTo({
						url: '../../pagesLogin/login/login'
					})
				}
			}
		},
		computed: {
			...mapState('indexStore', ['userData'])
		}
	}
</script>

<style lang="scss">
	/deep/.u-skeleton__wrapper {
		height: 100% !important;
		align-items: center;
	}

	/deep/.user_data {
		height: 300rpx;
		width: 95%;
		margin: 0 auto;
		border-radius: 20rpx;
		background-color: white;
		margin-top: 20rpx;
		display: flex;
		overflow: hidden;
		background-color: #00CFC5;

		.user_main_data {
			display: flex;
			flex-direction: row;
			width: 100%;
			height: 100%;
		}

		.user_img {
			width: 40%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;

			.user_img_redios {
				width: 80%;
				height: 80%;
				overflow: hidden;
				border-radius: 50%;
				background-color: #fff;
			}
		}

		.user_titel {
			width: 60%;
			height: 100%;

			.user_titel_main {
				width: 90%;
				height: 30%;
				background-color: #00A2F0;
				border-radius: 10rpx;
				margin: 0 auto;
				margin-top: 30rpx;
				color: black;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 50rpx;
				font-weight: 700;
			}

			.user_titel_center {
				padding: 20rpx;
				font-size: 30rpx;
				margin-top: 10rpx;
				color: black;
			}

			.user_titel_timer {
				padding-left: 20rpx;
				font-size: 30rpx;
				color: black;
			}
		}
	}

	.my_options {
		width: 95%;
		height: 100rpx;
		margin: 0 auto;
		border-radius: 20rpx;
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 20rpx;
		overflow: hidden;
		background-color: #00CFC5;

		.iconmargin {
			margin-right: 10rpx;
		}
	}

	page {
		background-color: #F9F871;
	}
</style>
